﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Remoting.Messaging;
using UnityCore;
using StoreClientModule;

//StoreClientModule (Uses OdbcAccess API)
namespace StoreClientModuleEx
{
    //Data Structures
    public struct StockManagerType
    {
        public int fID;
        public string fUser;
        public string fBarcode;
        public string fDescription;
        public string fDate;
        public float fQty;
        public string fUnit;
        public string fStamp;
        public int mPage;
        public int mCnt;
        public DataTable dt;

    }
    public struct GenericSearchType
    {
        public string Keyword;
        public int PageNum;
        public int Qty;
    }

    public static class ComEx
    {
        public static OdbcDetails LocSvr = new OdbcDetails() { DB = Common.lDatabase, SVR = Common.lServer, UID = Common.lUID, PWD = Common.lPWD, VER = OdbcVersion.v510 };
    }

    //Implementations
    public class StockManager : UnityProcessCall
    {
        DataTable _LogTable = new DataTable();
        DataTable _StockTable = new DataTable();

        DataTable _UnitTable = new DataTable();
        DataTable _ContentTable = new DataTable();

        DataRow[] foundData = new DataRow[0];

        DataTable _Products = new DataTable();
        SalesItemType[] _Items = new SalesItemType[0];

        object[] _UnitList = new object[0];
        object[] _BarcodeList = new object[0];
        object[] _DescriptionList = new object[0];
        //Sync Operation
        void pAddLog(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "AddLog" };

            StockManagerType smt = (StockManagerType)p;
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                db.Connect();
                db.Query("SELECT * FROM tblstocklog ORDER BY fDate DESC");
                db.Add(new object[] { DBNull.Value, smt.fUser, smt.fBarcode, smt.fDate, smt.fQty, smt.fUnit, DBNull.Value });
                db.Commit();
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pAddLogArray(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "AddLog" };

            StockManagerType[] smt = (StockManagerType[])p;
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                db.Connect();
                db.Query("SELECT * FROM tblstocklog ORDER BY fDate DESC");
                for (int i = 0; i < smt.Length; i++)
                {
                    db.Add(new object[] { DBNull.Value, smt[i].fUser, smt[i].fBarcode, smt[i].fDate, smt[i].fQty, smt[i].fUnit, DBNull.Value });
                }
                db.Commit();
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pGetLog(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "GetLog" };
            foundData = new DataRow[0];
            StockManagerType smt = (StockManagerType)p;
            //OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                //db.Connect();
                if (smt.fDate != "")
                {
                    smt.dt.Select("fDate LIKE '" + smt.fDate + "%' AND " +
                        "fBarcode LIKE '%" + smt.fBarcode + "%' AND " +
                        "fUser LIKE '%" + smt.fUser + "%' ORDER BY fDate DESC, fUser ASC, fBarcode ASC " +
                        "LIMIT " + ((smt.mPage - 1) * smt.mCnt) + "," + smt.mCnt);
                    /*db.Query("SELECT fUser, fBarcode, fAmount, fUnit FROM tblstocklog WHERE " +
                        "fDate LIKE '" + smt.fDate + "%' AND " +
                        "fBarcode LIKE '%" + smt.fBarcode + "%' AND " +
                        "fUser LIKE '%" + smt.fUser + "%' ORDER BY fDate DESC, fUser ASC, fBarcode ASC " +
                        "LIMIT " + ((smt.mPage - 1) * smt.mCnt) + "," + smt.mCnt);*/
                }
                else
                {
                    foundData = smt.dt.Select("fUser LIKE '%" + smt.fUser + "%' AND " +
                       "fBarcode LIKE '%" + smt.fBarcode + "%'");// ORDER BY fDate DESC, fUser ASC, fBarcode ASC " +
                       //"LIMIT " + ((smt.mPage - 1) * smt.mCnt) + "," + smt.mCnt);
                    
                    /*
                    db.Query("SELECT fUser, fBarcode, fAmount, fUnit FROM tblstocklog WHERE " +
                       "fUser LIKE '%" + smt.fUser + "%' AND " +
                       "fBarcode LIKE '%" + smt.fBarcode + "%' ORDER BY fDate DESC, fUser ASC, fBarcode ASC " +
                       "LIMIT " + ((smt.mPage - 1) * smt.mCnt) + "," + smt.mCnt);
                     */ 
                }
                
                //db.Export(ref _LogTable);
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pGetAutoUnit(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "GetAutoUnit" };
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                db.Connect();
                db.Query("SELECT fUnit FROM tblunits");
                db.Export(ref _UnitList, "fUnit");
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pGetAutoBarcode(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "GetAutoBarcode" };
            string str = (string)p;
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                object locker = new object();
                lock (locker)
                {
                    db.Connect();
                    db.Query("SELECT fBarcode, fDescription FROM tblproducts WHERE fBarcode LIKE '%" + str + "%' ORDER BY fBarcode ASC LIMIT 0,10");
                    db.Export(ref _BarcodeList, "fBarcode");
                    db.Export(ref _DescriptionList, "fDescription");

                    if (_BarcodeList.Length == 0) _BarcodeList = new object[] { "No match found!" };
                    if (_DescriptionList.Length == 0) _DescriptionList = new object[] { "No match found!" };
                }
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pAddStockWhole(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "AddStockWhole" };

            StockManagerType[] smt = (StockManagerType[])p;
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                db.Connect();
                float oQty = 0f;
                for (int item = 0; item < smt.Length; item++)
                {
                    oQty = smt[item].fQty;
                    db.Query("SELECT * FROM tblproductstockwhole WHERE fBarcode='" + smt[item].fBarcode + "'");
                    if (db.Count == 0)
                    {

                        db.Add(new object[] { DBNull.Value, smt[item].fBarcode, smt[item].fDescription, (float)Math.Round((decimal)smt[item].fQty, 0), DBNull.Value });
                        db.Commit();
                    }
                    else if (db.Count == 1)
                    {
                        smt[item].fQty += float.Parse(db.GetField(0, "fQty").ToString());
                        db.Update(0, new object[] { (float)Math.Round((decimal)smt[item].fQty, 0) }, new string[] { "fQty" });
                        db.Commit(ConflictOption.CompareRowVersion);
                    }
                    if (oQty < 1)
                    {
                        smt[item].fUser += " - Deducted";
                    }
                    else
                    {
                        smt[item].fUser += " - Added";
                    }
                    smt[item].fQty = Math.Abs(oQty);
                    smt[item].fUnit = "PCS";
                }
                if (smt.Length > 0)
                {
                    object or;
                    pAddLogArray(smt, out or);
                }
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pAddStockRetail(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "AddStockRetail" };

            StockManagerType[] smt = (StockManagerType[])p;
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                db.Connect();
                float oQty = 0f;
                string oUnit= "";
                for (int item = 0; item < smt.Length; item++)
                {
                    oUnit = smt[item].fUnit;
                    oQty = smt[item].fQty;
                    //Check Unit
                    db.Query("SELECT * FROM tblunits WHERE fUnit='" + smt[item].fUnit + "'");
                    if (db.Count == 1)
                    {
                        //if Units are different perform conversion
                        string fUnit = db.GetField(0, "fDefault").ToString();
                        if (fUnit != smt[item].fUnit)
                        {
                            if (fUnit.Equals("LTR")) smt[item].fQty = smt[item].fQty * Common.GetVolumeFactor(smt[item].fUnit, fUnit);
                            if (fUnit.Equals("M")) smt[item].fQty = smt[item].fQty * Common.GetLengthFactor(smt[item].fUnit, fUnit);
                            if (fUnit.Equals("KG")) smt[item].fQty = smt[item].fQty * Common.GetWeightFactor(smt[item].fUnit, fUnit);
                            smt[item].fUnit = fUnit;
                        }
                    }
                    
                    db.Query("SELECT * FROM tblproductstockretail WHERE fBarcode='" + smt[item].fBarcode + "'");
                    if (db.Count == 0)
                    {
                        db.Add(new object[] { DBNull.Value, smt[item].fBarcode, smt[item].fDescription, (float)Math.Round((decimal)smt[item].fQty, 4), smt[item].fUnit, DBNull.Value });
                        db.Commit();
                    }
                    else if (db.Count == 1)
                    {
                        smt[item].fQty += float.Parse(db.GetField(0, "fQty").ToString());
                        db.Update(0, new object[] { (float)Math.Round((decimal)smt[item].fQty, 4) }, new string[] { "fQty" });
                        db.Commit(ConflictOption.CompareRowVersion);
                    }
                    if (oQty < 1)
                    {
                        smt[item].fUser += " - Deducted";
                    }
                    else
                    {
                        smt[item].fUser += " - Added";
                    }
                    smt[item].fQty = Math.Abs(oQty);
                    smt[item].fUnit = oUnit;
                }
                if (smt.Length > 0)
                {
                    object or;
                    pAddLogArray(smt, out or);
                }
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pGetStockInfo(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "GetStockInfo" };
            StockManagerType smt = (StockManagerType)p;
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                object locker = new object();
                lock (locker)
                {
                    _StockTable = new DataTable();
                    _StockTable.Columns.Add("fBarcode");
                    _StockTable.Columns.Add("fDescription");
                    _StockTable.Columns.Add("fStockWhole");
                    _StockTable.Columns.Add("fStockRetail");
                    _StockTable.Columns.Add("fPCode");

                    db.Connect();
                    string tbl = "fQty FROM tblproductstockwhole";
                    if (smt.fID == 1) tbl = "fQty, fUnit FROM tblproductstockretail";
                    db.Query("SELECT fBarcode, fDescription," + tbl + " WHERE fDescription LIKE '%" + smt.fBarcode + "%' OR fBarcode LIKE '%" + smt.fBarcode +
                        "%' ORDER BY fBarcode ASC, fDescription ASC LIMIT " + (((int)smt.fQty-1) * 50) + ",50");

                    for (int i = 0; i < db.Count; i++)
                    {
                        DataRow dr = _StockTable.NewRow();
                        dr["fBarcode"] = db.GetField(i, "fBarcode");
                        dr["fDescription"] = db.GetField(i, "fDescription");
                        if (smt.fID == 0) dr["fStockWhole"] = db.GetField(i, "fQty") + " PCS";
                        if (smt.fID == 1) dr["fStockRetail"] = db.GetField(i, "fQty") + " " + db.GetField(i, "fUnit");

                        _StockTable.Rows.Add(dr);
                    }
                    
                }
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pSearchProduct(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "SearchProduct" };
            GenericSearchType gst = (GenericSearchType)p;
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                db.Connect();
                db.Query("SELECT fBarcode, fDescription, fSupplier, fContent, fUnit FROM tblproducts WHERE " +
                    "fBarcode LIKE '%" + gst.Keyword + "%' OR " +
                    "fDescription LIKE '%" + gst.Keyword + "%' OR " +
                    "fSupplier LIKE '%" + gst.Keyword + "%' OR " +
                    "fContent LIKE '%" + gst.Keyword + "%' OR " +
                    "fUnit LIKE '%" + gst.Keyword + "%' LIMIT "+((gst.PageNum-1) * gst.Qty)+"," +gst.Qty);
                db.Export(ref _Products);    
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pGetStockInfoForPrinting(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "GetStockInfoForPrinting" };
            string smt = (string)p;
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                object locker = new object();
                lock (locker)
                {
                    _StockTable = new DataTable();
                    _StockTable.Columns.Add("fBarcode");
                    _StockTable.Columns.Add("fDescription");
                    _StockTable.Columns.Add("fStockWhole");
                    _StockTable.Columns.Add("fStockRetail");
                    _StockTable.Columns.Add("fPCode");
                    _StockTable.Columns.Add("fAmount");
                    db.Connect();
                    db.Query("SELECT fBarcode, fDescription, fQty FROM tblproductstockwhole WHERE fDescription LIKE '%" + smt + "%' OR fBarcode LIKE '%" + smt +
                        "%' ORDER BY fBarcode ASC, fDescription ASC");

                    for (int i = 0; i < db.Count; i++)
                    {
                        DataRow dr = _StockTable.NewRow();
                        dr["fBarcode"] = db.GetField(i, "fBarcode");
                        dr["fDescription"] = db.GetField(i, "fDescription");
                        dr["fStockWhole"] = db.GetField(i, "fQty");

                        _StockTable.Rows.Add(dr);
                    }
                    if (_StockTable.Rows.Count != 0)
                    {
                        string retQuery = "SELECT fBarcode, fDescription, fQty, fUnit FROM tblproductstockretail WHERE ";
                        for (int i = 0; i < _StockTable.Rows.Count; i++)
                        {
                            retQuery += "fBarcode='" + _StockTable.Rows[i]["fBarcode"].ToString() + "'";
                            if (i < _StockTable.Rows.Count - 1) retQuery += " OR ";
                        }
                        db.Query(retQuery);
                        for (int i = 0; i < db.Count; i++)
                        {
                            for (int j = 0; j < _StockTable.Rows.Count; j++)
                            {
                                if (_StockTable.Rows[j]["fBarcode"].ToString() == db.GetField(i, "fBarcode").ToString())
                                {
                                    _StockTable.Rows[j]["fStockRetail"] = db.GetField(i, "fQty") + " " + db.GetField(i, "fUnit"); ;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {

                    }
                    //string str = "SELECT f
                }
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        //Stocks and Inventory AI
        void pUpdateStock(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "UpdateStock" };

            SalesItemType[] sit = (SalesItemType[])p;
            List<StockManagerType> smtWhole = new List<StockManagerType>();
            List<StockManagerType> smtRetail = new List<StockManagerType>();

            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                db.Connect();
                float diff = 0f;
                object or;
                DataRow[] dra = new DataRow[0];
                for (int item = 0; item < sit.Length; item++)
                {
                    //Convert the quantity to default quantity.
                    dra = new DataRow[0];
                    diff = 0f;
                    or= null;
                    if (sit[item].fUnit != "PCS")
                    {
                        
                        dra = _UnitTable.Select("fUnit LIKE '%" + sit[item].fUnit + "%'");
                        if (dra[0]["fDefault"].ToString() == "LTR") sit[item].fQty = (float.Parse(sit[item].fQty) * Common.GetVolumeFactor(sit[item].fUnit, dra[0]["fDefault"].ToString())).ToString();
                        if (dra[0]["fDefault"].ToString() == "M") sit[item].fQty = (float.Parse(sit[item].fQty) * Common.GetLengthFactor(sit[item].fUnit, dra[0]["fDefault"].ToString())).ToString();
                        if (dra[0]["fDefault"].ToString() == "KG") sit[item].fQty = (float.Parse(sit[item].fQty) * Common.GetWeightFactor(sit[item].fUnit, dra[0]["fDefault"].ToString())).ToString();
                        diff = float.Parse(sit[item].fQty) % float.Parse(sit[item].fContentQty);
                    }
                    else
                    {
                        dra = _UnitTable.Select("fUnit LIKE '%" + sit[item].fContentUnit + "%'");
                        if (dra[0]["fDefault"].ToString() == "PCS") diff = float.Parse(sit[item].fQty) % float.Parse(sit[item].fContentQty);
                    }

                    //Identify whether to be deducted from WholeStock or Retail Stock
                    
                    
                    if (diff == 0)
                    {
                        if (sit[item].fUnit.ToUpper().Contains("PCS")  && !sit[item].fContentUnit.ToUpper().Contains("PCS") )
                        {
                            smtWhole.Add(new StockManagerType()
                            {
                                fUser = Common.Username,
                                fDate = UnityCore.OdbcAccess.ToOdbcDateTime(DateTime.Now),
                                fBarcode = sit[item].fBarcode,
                                fDescription = sit[item].fDescription,
                                fQty = float.Parse(sit[item].fQty) * -1f
                            });
                            
                        }
                        else
                        {
                            smtWhole.Add(new StockManagerType()
                            {
                                fUser = Common.Username,
                                fDate = UnityCore.OdbcAccess.ToOdbcDateTime(DateTime.Now),
                                fBarcode = sit[item].fBarcode,
                                fDescription = sit[item].fDescription,
                                fQty = (float.Parse(sit[item].fQty) / float.Parse(sit[item].fContentQty)) * -1f
                            });
                        }
                    }
                    else
                    {
                        db.Query("SELECT * FROM tblproductstockretail WHERE fBarcode='" + sit[item].fBarcode + "'");
                        if (db.Count == 1)
                        {
                            float curQty = float.Parse(db.GetField(0, "fQty").ToString());
                            if (curQty >= float.Parse(sit[item].fQty))
                            {
                                //db.SetField(0, "fQty", Math.Round((decimal)(curQty - float.Parse(sit[item].fQty)), 4));
                                db.Update(0, new object[] { Math.Round((decimal)(curQty - float.Parse(sit[item].fQty)), 4) }, new string[] { "fQty" });
                                db.Commit(ConflictOption.CompareRowVersion);

                                pAddLog(new StockManagerType()
                                {
                                    fUser = Common.Username + " - Deducted",
                                    fDate = UnityCore.OdbcAccess.ToOdbcDateTime(DateTime.Now),
                                    fBarcode = sit[item].fBarcode,
                                    fDescription = sit[item].fDescription,
                                    fQty = float.Parse(sit[item].fQty),
                                    fUnit = dra[0]["fDefault"].ToString()
                                }, out or);
                                
                            }
                            else
                            {
                                float fac = float.Parse(sit[item].fQty) % float.Parse(sit[item].fContentQty);
                                float ret = float.Parse(sit[item].fContentQty) - fac;
                                float whole = ((float.Parse(sit[item].fQty) - fac) / float.Parse(sit[item].fContentQty)) + 1;

                                //db.SetField(0, "fQty", Math.Round((decimal)(curQty + ret), 4));
                                db.Update(0, new object[] { Math.Round((decimal)(curQty + ret), 4) }, new string[] { "fQty" });
                                db.Commit(ConflictOption.CompareRowVersion);
                                pAddLog(new StockManagerType()
                                {
                                    fUser = Common.Username + " - Deducted",
                                    fDate = UnityCore.OdbcAccess.ToOdbcDateTime(DateTime.Now),
                                    fBarcode = sit[item].fBarcode,
                                    fDescription = sit[item].fDescription,
                                    fQty = float.Parse(sit[item].fQty),
                                    fUnit = dra[0]["fDefault"].ToString()
                                }, out or);
                           
                                smtWhole.Add(new StockManagerType()
                                {
                                    fUser = Common.Username,
                                    fDate = UnityCore.OdbcAccess.ToOdbcDateTime(DateTime.Now),
                                    fBarcode = sit[item].fBarcode,
                                    fDescription = sit[item].fDescription,
                                    fQty = whole * -1f
                                });
                            }
                        }
                        else
                        {
                            float fac = float.Parse(sit[item].fQty) % float.Parse(sit[item].fContentQty);
                            float ret = float.Parse(sit[item].fContentQty) - fac;
                            float whole = ((float.Parse(sit[item].fQty) - fac) / float.Parse(sit[item].fContentQty)) + 1;

                            smtRetail.Add(new StockManagerType()
                            {
                                fUser = Common.Username,
                                fDate = UnityCore.OdbcAccess.ToOdbcDateTime(DateTime.Now),
                                fBarcode = sit[item].fBarcode,
                                fDescription = sit[item].fDescription,
                                fQty = ret,
                                fUnit = dra[0]["fDefault"].ToString()
                            });

                            smtWhole.Add(new StockManagerType()
                            {
                                fUser = Common.Username,
                                fDate = UnityCore.OdbcAccess.ToOdbcDateTime(DateTime.Now),
                                fBarcode = sit[item].fBarcode,
                                fDescription = sit[item].fDescription,
                                fQty = whole * -1f
                            });
                        }
                        
                    }
                    
                }
                object ol;
                if (smtWhole.Count > 0) pAddStockWhole(smtWhole.ToArray(), out o);
                if (smtRetail.Count > 0) pAddStockRetail(smtRetail.ToArray(), out o);
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        void pPreloadUnits(object p, out object o)
        {
            UnityProcess proc = new UnityProcess() { ID = "PreloadUnits" };
            OdbcAccess db = new OdbcAccess(ComEx.LocSvr);
            try
            {
                db.Connect();
                db.Query("SELECT fUnit, fDefault FROM tblunits");
                db.Export(ref _UnitTable);
            }
            catch (Exception ex)
            {
                proc.HasError = true;
                proc.Msg = ex.Message;
            }
            finally
            {
                o = proc;
            }
        }
        //Exposed Members
        public void AddLog(StockManagerType smt)
        {
            ThreadHandler th = new ThreadHandler(pAddLog);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(smt, out o, ac, null);
        }
        public void AddLog(StockManagerType[] smt)
        {
            ThreadHandler th = new ThreadHandler(pAddLogArray);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(smt, out o, ac, null);
        }
        public void GetLog(StockManagerType smt)
        {
            ThreadHandler th = new ThreadHandler(pGetLog);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(smt, out o, ac, null);
        }
        public void GetAutoUnit()
        {
            ThreadHandler th = new ThreadHandler(pGetAutoUnit);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(null, out o, ac, null);
        }
        public void GetAutoBarcode(string str)
        {
            ThreadHandler th = new ThreadHandler(pGetAutoBarcode);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(str, out o, ac, null);
        }
        public void AddStockWhole(StockManagerType[] smt)
        {
            ThreadHandler th = new ThreadHandler(pAddStockWhole);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(smt, out o, ac, null);
        }
        public void AddStockRetail(StockManagerType[] smt)
        {
            ThreadHandler th = new ThreadHandler(pAddStockRetail);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(smt, out o, ac, null);
        }
        public void GetStockInfo(StockManagerType str)
        {
            ThreadHandler th = new ThreadHandler(pGetStockInfo);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(str, out o, ac, null);
        }
        public void SearchProduct(GenericSearchType gst)
        {
            ThreadHandler th = new ThreadHandler(pSearchProduct);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(gst, out o, ac, null);
        }
        public void PreloadUnits()
        {
            ThreadHandler th = new ThreadHandler(pPreloadUnits);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(null, out o, ac, null);
        }
        public void UpdateStock(SalesItemType[] sit)
        {
            ThreadHandler th = new ThreadHandler(pUpdateStock);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(sit, out o, ac, null);
        }
        public void GetStockInfoForPrinting(string str)
        {
            ThreadHandler th = new ThreadHandler(pGetStockInfoForPrinting);
            AsyncCallback ac = new AsyncCallback(GenericCB);
            object o;
            IAsyncResult ar = th.BeginInvoke(str, out o, ac, null);
        }
        public DataTable LogTable
        {
            get { return _LogTable; }
        }
        public DataRow [] LogRow
        {
            get { return foundData; }
        }
        public DataTable StockTable
        {
            get { return _StockTable; }
        }
        public DataTable Products
        {
            get { return _Products; }
        }
        public SalesItemType[] ItemsToUpdate
        {
            set { _Items = value; }
            get { return _Items; }
        }
        public object[] UnitList
        {
            get { return _UnitList; }
        }
        public object[] BarcodeList
        {
            get { return _BarcodeList; }
        }
        public object[] DescriptionList
        {
            get { return _DescriptionList; }
        }

    }
   
}